package com.sky.controller;

import com.sky.anotion.validator.AddGroup;
import com.sky.anotion.validator.UpdateGroup;
import com.sky.constant.MessageConstant;
import com.sky.dto.common.PageRequest;
import com.sky.dto.common.ParamsID;
import com.sky.dto.common.QueryParams;
import com.sky.entity.Dict;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DicSercice;
import com.sky.vo.DictVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/dict")
@Slf4j
@Validated
public class DicController {
    @Autowired
    private DicSercice dicSercice;

    /**
     * 分页查询-获取所有的分类字典数据
     * @param queryParams 分页查询
     * @return
     */
    @PostMapping("/getDicDataAll")
    public Result<PageResult> getAllDic(@RequestBody PageRequest<QueryParams> queryParams) {
        log.info("获取所有的分类字典");
        PageResult pageResult = dicSercice.getDicDataAll(queryParams);
        return Result.success(pageResult, MessageConstant.OK_MESSAGE);
    }
    /**
     * 获取字典数据-- 分类下的字典数据
     * @param queryParams 字典名称
     * @return 树形结构的数据
     */
    @PostMapping("/getDicData")
    public Result<PageResult> getDicData(@RequestBody PageRequest<QueryParams> queryParams) {
        log.info("获取字典数据，dicCode: {}", queryParams);
        PageResult pageResult =  dicSercice.getDicData(queryParams);
        return Result.success(pageResult, MessageConstant.OK_MESSAGE);
    }

    /**
     * 获取字典树形数据-- 不带分页记录
     * @return 树形结构
     */
    @PostMapping("/getDicDataCode")
    public Result<List<DictVO>> getDictCodeTree(@RequestBody Map<String, String> params){
        log.info("获取字典树形数据，dictCode: {}", params.get("dictCode"));
        List<DictVO> result = dicSercice.getDicDataTree(params.get("dictCode"));
        return Result.success(result, MessageConstant.OK_MESSAGE);
    }
    /**
     * 增加字典数据
     * @param dict 字典数据
     */
    @PostMapping("/addDicData")
    public Result<String> addDicData(@RequestBody @Validated(AddGroup.class) Dict dict) {
        log.info("增加字典数据，dict: {}", dict);
        dicSercice.addDicData(dict);
        return Result.success(null, MessageConstant.OK_MESSAGE);
    }
    /**
     * 删除字典数据
     */
    @PostMapping("/deleteDicData")
    public Result<String> deleteDicData(@RequestBody ParamsID ids) {
        log.info("删除字典数据，id: {}", ids);
        dicSercice.deleteDicData(ids);
        return Result.success(null, MessageConstant.OK_MESSAGE);
    }
    /**
     * 更新字典数据
     * @param dict 字典数据
     */
    @PutMapping("/updateDicData")
    public Result<String> updateDicData(@RequestBody @Validated(UpdateGroup.class) Dict dict) {
        log.info("更新字典数据，dict: {}", dict);
        dicSercice.updateDicData(dict);
        return Result.success(null, MessageConstant.OK_MESSAGE);
    }
}
